package com.atguigu.gulimall.gulimallorder.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.atguigu.gulimall.gulimallorder.vo.PayVo;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000118658880";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDe0piRLCiNvrLE1mfuyQekrw7jah2js7qW77nrATzuIol67StotmWMFEyW1PslQR9MlzqpdNsUt80ud+5gFW1MgYC1nK06z2wqewwc7O5m1bAQalavrKThakFjYncB4v9MYAtQgrwi/1QwCaPzn5pIkdf++g9byseh22RUkxtvKFhXxdyBaYkrHXGJGUt+YbSi3/g8zsNEnKl+X0Q0kA2nWvnvGuFuWC+/hKNhiD5CFKKEtMAKT1C2u7pY0KtgHG46VrJU2W9tPg97qs6WolFGPgWZTfvwccW2pnkCT1s6K3TRyzZJrkutNhiayYuC3eLnx45QNblK7cui3E/fYDx3AgMBAAECggEAMgfBH991X5Yw14d4oKHcqHpx8lfyPb5uJ9O9v+lwP9vbWJGGDKXfmRs5sygxF8VOfMj77+5//l/inwtop4RIHUR6nMyIDMtj8wnbx2bfhJF4TcdCnYE7KC3Obr0+EVthjUmHYNVp3sXLplmTyXDNmHu+rs6AwlB+kHYNSfwZzdQq+P2gP1d3VKCf5Rz3T4g48l1Agg3CkY7+nbNmDvauPCcv12IHWmKDed87sFVlppk/i8vCXl6JqpL6unMp96d78e8o4r/6LJ3cmznafTg0Dy8MkbgvxAFVIJqDqH4XtMjh1pTVELltsGTsnQn+mKa5QLgwCzci5gXeDKXWzId0QQKBgQDvL6jfIJBQW2ikPnfTOEb5mg4ibt2Hkb9coJkc7hNLJRzcj6z9T9GFxu2Ib20hMxIZFU8oStg4AoNKXr+6OPAC5ZpG9eRRCAhYW4hdR4z7NaS/Ux/9PW46X3p1FWl6Hp6LidLC8+c+tlNpM8cVtUAlKS4p3GdjD08jW2GO20hxIQKBgQDufHZALyZQjpJmBrblVAw2hu25og/XamUv1e15rfznexFRJ4RSGORDbNfvVqSWKSRskoiuGeFSway3El844oidM5et/IbE/4XBi4CMka9rIEoEYKUj1sfjDvHnci6u5sHOKw/kBkf08SI8r5AUb+jCeAdG/etaeZwviJlmbBtClwKBgHKydRWCZRHTs6tHNWi5TPe2k16ZsWuoTY6aiLHhzKi1RFDUKQ1T8FJpYVCStk88UUpr+IiG04aQVesc4m3njdilx8SNd44P/wh35D1u6UExqS4CK43hnnEpheyNdwYTls2tKr9H/LUK85a4OJjFUNxBEh0Kzx0mozl+QZsitmthAoGBAOX+LABcOEQqKu+v/ASQGLMhTvPeYg6qMel/E2VssOEAo8OzK34NOV35AwpzcYjLJrB6ZMwac13fH9TyJ9jxRzTrw5TsW4ByuKjeA1wNR7I1UoyEM96rTNwh1pMRYAoWysMsbkfG8vA6+Hy0B2xePIgHP2VsjeQUMdb5ko7fJS8DAoGBAIY9AbC/HrEumXcDzddgy3EQZK9ABdbZ6W9epuvj4w0FjBob1w600aWNKhLdPLLxk3qOSVPonxsvzwNetJrrgWKxjDjhJjwaHuoy9QAuYhwd0pDVyjxsRqzn0BcnWs11FIdGn7ARSoRboFqR3egCFzQeyjW9YK4WFe1cRvCkXZkJ";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAle6FX/P6yNwBTvdf8as/zBFm/xnR30HRzrpale88atTnzWf8CEcoekv0v0VCE2U37Xm0vnHka2IiNnVvE2t1SrVJEfQISeW2b9P1MWZMJeAqjAu/AjPVcNoGscJ1f/sT9mbBbextiKXoyYDrTaiSv4jDEDwdR10MtDr4ofCA/z63mRk4sEQblnr9JwbaXB+KLj+xLz0kz+TLCCVfvjj9gfnKMjh+7R2hBhfjWnvSrI5AeReMbiNQf4grHBEMenUUY/yeGyScs/yNCf50n0v9ZkOJUsZFAX5MyH19bsn+l9/Snp5oEJplEUqJdCR+16ZLNR6aSVLguqYXKRWyQ5Q0mQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://0889-124-160-216-214.ngrok.io/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url= "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //超时时间
    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
